<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);

$exc = new exchange($ecs->table("goods_inquiry"), $db, 'id', 'brand_name');

// 关联条件，目的是将商品名称、国家、品牌、规格、型号、订货码、物料类型这几个条件完全相同时才认为是同个商品。后期如果有变动时，这个查询条件需要改下，还在删除那个地方也要处理
$link_condition = " goods_name = a.goods_name and nation = a.nation and brand_name = a.brand_name and specification = a.specification and model = a.model and order_code = a.order_code and materials_type = a.materials_type and ";
	
/*------------------------------------------------------ */
//-- 品牌列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here',      $_LANG['goods_inquiry_info']);
    $smarty->assign('action_link',  array('text' => $_LANG['01_400add_goods_inquiry_info'], 'href' => 'goods_inquiry.php?act=add'));
    $smarty->assign('full_page',    1);

    $brand_list = getGoodsInquiryList();
    
    $smarty->assign('cat_list', cat_list(0, 0, true, 1)); // 取得国家
    $smarty->assign('cat_brand_list',        get_cat_brand_list()); // 取得国家品牌

    $smarty->assign('entityList',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);

    assign_query_info();
    $smarty->display('goods_inquiry_list.htm');
}

/*------------------------------------------------------ */
//-- 录入询价商品信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
	//TODO 
    //admin_priv('brand_manage');

    $smarty->assign('ur_here',     $_LANG['01_400add_goods_inquiry_info']);
    $smarty->assign('action_link', array('text' => $_LANG['goods_inquiry_info'], 'href' => 'goods_inquiry.php?act=list'));
    $smarty->assign('form_action', 'insert');
    
    $smarty->assign('cat_list', cat_list(0, 0, true, 1)); // 取得国家
    $smarty->assign('cat_brand_list',        get_cat_brand_list()); // 取得国家品牌
    
    assign_query_info();
    $smarty->display('goods_inquiry_info.htm');
}
elseif ($_REQUEST['act'] == 'insert')
{
    /*检查品牌名是否重复*/
	//TODO
    //admin_priv('brand_manage');

	//这里要注意下，页面传来的是ID，保存库里的为汉字，这在这里转换下
    $sql = "SELECT cat_name FROM " .$ecs->table('category'). " WHERE cat_id='$_POST[nation]'";
    $entity = $db->GetRow($sql);
	
	
	$sql = "INSERT INTO ".$ecs->table('goods_inquiry')."(goods_name, nation, brand_name, specification, model, order_code, materials_type, no_tax_price, update_time) ".
	"VALUES ('$_POST[goods_name]', '" .$entity['cat_name']. "', '$_POST[brand_name]', '$_POST[specification]', '$_POST[model]', '$_POST[order_code]', '$_POST[materials_type]', '$_POST[no_tax_price]', '" . gmtime() . "')";

    $db->query($sql);

    admin_log($_POST['order_code'],'add','goods_inquiry');

    /* 清除缓存 */
    clear_cache_files();

    $link[0]['text'] = $_LANG['continue_add'];
    $link[0]['href'] = 'goods_inquiry.php?act=add';

    $link[1]['text'] = $_LANG['back_list'];
    $link[1]['href'] = 'goods_inquiry.php?act=list';

    sys_msg($_LANG['brandadd_succed'], 0, $link);
}

/*------------------------------------------------------ */
//-- 编辑或历史详细查看
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit' || $_REQUEST['act'] == 'history_view')
{
    /* 权限判断 */
	//TODO
    //admin_priv('brand_manage');
    $sql = "SELECT id, goods_name, nation, brand_name, specification, model, order_code, materials_type, no_tax_price, update_time ".
            "FROM " .$ecs->table('goods_inquiry'). " WHERE id='$_REQUEST[id]'";
    $entity = $db->GetRow($sql);
    
    //处理时间
    $entity['update_time'] = local_date($_CFG['time_format'], $entity['update_time']);
    
    if($_REQUEST['act'] == 'edit') {
    	$ur_here = $_LANG['inquiry_edit'];
    	$text = $_LANG['goods_inquiry_info'];
    	$href = 'goods_inquiry.php?act=list&' . list_link_postfix();
    } else {
    	$ur_here = $_LANG['history_goods_inquiry'];
    	$text = $_LANG['01_430history_goods_inquiry'];
    	$href = 'goods_inquiry.php?act=history_query&' . list_link_postfix();
    	
    	$smarty->assign('history_query', "history_query");
    }

    $smarty->assign('ur_here',     $ur_here);
    $smarty->assign('action_link', array('text' => $text, 'href' => $href));
    $smarty->assign('entity',       $entity);
    $smarty->assign('form_action', 'updata');
    //$smarty->assign('cat_list', cat_list(0, $entity[cat_id], true, 1));
    

    assign_query_info();
    $smarty->display('goods_inquiry_info.htm');
}
elseif ($_REQUEST['act'] == 'updata')
{
    /* 权限判断 */
	//TODO
    //admin_priv('brand_manage');
    
	// 先查原来的
    $sql = "SELECT goods_name, nation, brand_name, specification, model, order_code, materials_type, no_tax_price, update_time ".
            "FROM " .$ecs->table('goods_inquiry'). " WHERE id='$_POST[id]'";
    $entity = $db->GetRow($sql);
	
    // 将原来的数据重新插入，同时更新价格与时间 
    $sql = "INSERT INTO ".$ecs->table('goods_inquiry')."(goods_name, nation, brand_name, specification, model, order_code, materials_type, no_tax_price, update_time) ".
	"VALUES ('$entity[goods_name]', '$entity[nation]', '$entity[brand_name]', '$entity[specification]', '$entity[model]', '$entity[order_code]', '$entity[materials_type]', '$_POST[no_tax_price]', '" . gmtime() . "')";

    $db->query($sql);
    
	/* 清除缓存 */
	clear_cache_files();
	
	admin_log($_POST['order_code'],'add','goods_inquiry');
	
	$link[0]['text'] = $_LANG['back_list'];
	$link[0]['href'] = 'goods_inquiry.php?act=list&' . list_link_postfix();
	$note = vsprintf($_LANG['goods_inquiry_edit_succed'], $_POST['brand_name']);
	sys_msg($note, 0, $link);
        
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
	//TODO 检查管理员权限，返回JSON格式数剧
    //check_authz_json('brand_manage');

    $id = intval($_GET['id']);

    // 通过ID查询到记录
    $sql = "SELECT goods_name, nation, brand_name, specification, model, order_code, materials_type, no_tax_price, update_time ".
            "FROM " .$ecs->table('goods_inquiry'). " WHERE id='$id'";
    $entity = $db->GetRow($sql);
    
   // 将条件符合的全部删除
    $sql = "DELETE FROM ".$ecs->table('goods_inquiry')." where goods_name = '$entity[goods_name]' and nation = '$entity[nation]' and brand_name = '$entity[brand_name]' and specification = '$entity[specification]' and model = '$entity[model]' and order_code = '$entity[order_code]' and materials_type = '$entity[materials_type]'";
    $db->query($sql);

    $url = 'goods_inquiry.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
	if($_REQUEST['history_query'] == 'history_query') {
		$brand_list = getGoodsInquiryList(null, null, true);
		$smarty->assign('history_query', "history_query");
	} else if($_REQUEST['history_query'] == 'goods_inquiry_list') {
		$brand_list = getAddGoodsInquiryList();
		$smarty->assign('history_query', "goods_inquiry_list");
	} else {	
		$brand_list = getGoodsInquiryList();
	}
    
    $smarty->assign('entityList',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);

    if($_REQUEST['history_query'] == 'goods_inquiry_list') {//添加商品的分页
    	make_json_result($smarty->fetch('goods_add_inquiry_list.htm'), '',
        	array('filter' => $brand_list['filter'], 'page_count' => $brand_list['page_count']));    	
    } else {
    	make_json_result($smarty->fetch('goods_inquiry_list.htm'), '',
        	array('filter' => $brand_list['filter'], 'page_count' => $brand_list['page_count']));    	
    }
}

/*------------------------------------------------------ */
//-- 询价查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'inquiry_query'){
	
	$smarty->assign('ur_here',      $_LANG['01_420add_goods_inquiry']);
	
	$smarty->assign('cat_list', cat_list(0, 0, true, 1)); // 取得国家
    $smarty->assign('cat_brand_list',        get_cat_brand_list()); // 取得国家品牌
	
	assign_query_info();
	$smarty->display('goods_inquiry_query.htm');
}

/*------------------------------------------------------ */
//-- 询价查询列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'inquiry_list'){
    $brand_list = getGoodsInquiryList("", 1000);
    
	$smarty->assign('ur_here',      $_LANG['goods_inquiry_info']);
    $smarty->assign('action_link',  array('text' => $_LANG['01_400add_goods_inquiry_info'], 'href' => 'goods_inquiry.php?act=add'));
    $smarty->assign('full_page',    1);

    $smarty->assign('entityList',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);

    assign_query_info();
	$smarty->display('goods_inquiry_query_list.htm');
}

/*------------------------------------------------------ */
//-- 历史询价查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'history_query'){
    $smarty->assign('ur_here',      $_LANG['history_goods_inquiry']);
    //$smarty->assign('action_link',  array('text' => $_LANG['01_430history_goods_inquiry'], 'href' => 'goods_inquiry.php?act=add'));
    $smarty->assign('full_page',    1);

    $brand_list = getGoodsInquiryList(null, null, true);

    $smarty->assign('entityList',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);

    $smarty->assign('cat_list', cat_list(0, 0, true, 1)); // 取得国家
    $smarty->assign('cat_brand_list',        get_cat_brand_list()); // 取得国家品牌
    
    $smarty->assign('history_query', "history_query");

    assign_query_info();
    $smarty->display('goods_inquiry_list.htm');
}

/*------------------------------------------------------ */
//-- 保存询价列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'save_list'){
	$arr = array();
	if(isset($_POST['goods_name'])){
		 foreach ($_POST['goods_name'] AS $key => $attr_id) {
		 	$temp_arr = array();
			$temp_arr['goods_name'] = $_POST['goods_name'][$key];
			$temp_arr['nation'] = $_POST['nation'][$key];
			$temp_arr['brand_name'] = $_POST['brand_name'][$key];
			$temp_arr['specification'] = $_POST['specification'][$key];
			$temp_arr['model'] = $_POST['model'][$key];
			$temp_arr['order_code'] = $_POST['order_code'][$key];
			$temp_arr['materials_type'] = $_POST['materials_type'][$key];
			$temp_arr['no_tax_price'] = $_POST['no_tax_price'][$key];
			$temp_arr['update_time'] = $_POST['update_time'][$key];
			$temp_arr['goods_num'] = $_POST['goods_num'][$key];
			$temp_arr['goods_date'] = $_POST['goods_date'][$key];
			$temp_arr['goods_desc'] = $_POST['goods_desc'][$key];
			$arr[] = $temp_arr;
		 }
	}	
	
	foreach ($arr as $cur) {
		$goods_name = $cur['goods_name'];
		$nation = $cur['nation'];
		$brand_name = $cur['brand_name'];
		$specification = $cur['specification'];
		$model = $cur['model'];
		$order_code = $cur['order_code'];
		$materials_type = $cur['materials_type'];
		$no_tax_price = $cur['no_tax_price'];
		$update_time = local_strtotime($cur['update_time']);
		$goods_count = $cur['goods_num'];
		$goods_delivery = $cur['goods_date'];
		$goods_desc = $cur['goods_desc'];
		//$send_email = $cur['send_email'];
		//$receive_email = $_SESSION[email];//登录人的邮箱
	    $sql = "SELECT email FROM " .$ecs->table('admin_user'). " WHERE user_id='$_SESSION[admin_id]'";
        $receive_email = $db->getOne($sql);
		
		$sql = "INSERT INTO ".$ecs->table('goods_inquiry_list')."(goods_name,nation,brand_name,specification,model,order_code,materials_type,no_tax_price,update_time,goods_count,goods_delivery,goods_desc,add_username,add_uid,receive_email,add_datetime) ".
		" VALUES ('$goods_name','$nation','$brand_name','$specification','$model','$order_code','$materials_type','$no_tax_price','$update_time','$goods_count','$goods_delivery','$goods_desc','$_SESSION[admin_name]','$_SESSION[admin_id]','$receive_email',".gmtime().")";
		
	    $db->query($sql);		
	}
	
    /* 清除缓存 */
    //clear_cache_files();

    $link[0]['text'] = $_LANG['01_420add_goods_inquiry'];
    $link[0]['href'] = 'goods_inquiry.php?act=inquiry_query';

    sys_msg($_LANG['add_inquiry_succed'], 0, $link);
}

/*------------------------------------------------------ */
//-- 添加商品询价列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'goods_inquiry_list'){
    $smarty->assign('ur_here',      $_LANG['goods_inquiry_list']);
    //$smarty->assign('action_link',  array('text' => $_LANG['01_430history_goods_inquiry'], 'href' => 'goods_inquiry.php?act=add'));
    $smarty->assign('full_page',    1);

    $brand_list = getAddGoodsInquiryList();

    $smarty->assign('entityList',   $brand_list['brand']);
    $smarty->assign('filter',       $brand_list['filter']);
    $smarty->assign('record_count', $brand_list['record_count']);
    $smarty->assign('page_count',   $brand_list['page_count']);
    
    $smarty->assign('cat_list', cat_list(0, 0, true, 1)); // 取得国家
    $smarty->assign('cat_brand_list',        get_cat_brand_list()); // 取得国家品牌
    
    $smarty->assign('history_query', "goods_inquiry_list");

    assign_query_info();
    $smarty->display('goods_add_inquiry_list.htm');
}

/*------------------------------------------------------ */
//-- 删除添加商品询价
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'goods_inquiry_list_delete'){
    $id = intval($_REQUEST['id']);

    $sql = "DELETE FROM ".$ecs->table('goods_inquiry_list')." where id = $id ";
    $db->query($sql);

    $url = 'goods_inquiry.php?act=query&' . str_replace('act=goods_inquiry_list_delete', '', $_SERVER['QUERY_STRING']);

    ecs_header("Location: $url\n");
    exit;	
}

/*------------------------------------------------------ */
//-- 添加商品询价编辑
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'goods_inquiry_list_edit'){
    /* 权限判断 */
	//TODO
    //admin_priv('brand_manage');
    $sql = "SELECT * FROM " .$ecs->table('goods_inquiry_list'). " WHERE id='$_REQUEST[id]'";
    $entity = $db->GetRow($sql);
    
    //处理时间
    $entity['update_time'] = local_date($_CFG['time_format'], $entity['update_time']);
    

    $ur_here = $_LANG['goods_inquiry_add'];
    $text = $_LANG['goods_inquiry_list'];
    $href = 'goods_inquiry.php?act=goods_inquiry_list&' . list_link_postfix();
    
    //$smarty->assign('history_query', "history_query");

    $smarty->assign('ur_here',     $ur_here);
    $smarty->assign('action_link', array('text' => $text, 'href' => $href));
    $smarty->assign('entity',       $entity);
    $smarty->assign('form_action', 'inquiry_email');
    //$smarty->assign('cat_list', cat_list(0, $entity[cat_id], true, 1));
    

    assign_query_info();
    $smarty->display('goods_inquiry_list_edit.htm');
}

/*------------------------------------------------------ */
//-- 发送邮件
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'inquiry_email'){
	$arr = array();
/*	// 这个是多个，前台控件要用数组命名方式形式，如name[]这样命名
	if(isset($_POST['goods_name'])){
		 foreach ($_POST['goods_name'] AS $key => $attr_id) {
		 	$temp_arr = array();
			$temp_arr['goods_name'] = $_POST['goods_name'][$key];
			$temp_arr['nation'] = $_POST['nation'][$key];
			$temp_arr['brand_name'] = $_POST['brand_name'][$key];
			$temp_arr['specification'] = $_POST['specification'][$key];
			$temp_arr['model'] = $_POST['model'][$key];
			$temp_arr['order_code'] = $_POST['order_code'][$key];
			$temp_arr['materials_type'] = $_POST['materials_type'][$key];
			$temp_arr['no_tax_price'] = $_POST['no_tax_price'][$key];
			$temp_arr['update_time'] = $_POST['update_time'][$key];
			$temp_arr['goods_num'] = $_POST['goods_num'][$key];
			$temp_arr['goods_date'] = $_POST['goods_date'][$key];
			$temp_arr['goods_desc'] = $_POST['goods_desc'][$key];
			$arr[] = $temp_arr;
		 }
	}
*/	
	
	$temp_arr = array();
	$temp_arr['goods_name'] = $_POST['goods_name'];
	$temp_arr['nation'] = $_POST['nation'];
	$temp_arr['brand_name'] = $_POST['brand_name'];
	$temp_arr['specification'] = $_POST['specification'];
	$temp_arr['model'] = $_POST['model'];
	$temp_arr['order_code'] = $_POST['order_code'];
	$temp_arr['materials_type'] = $_POST['materials_type'];
	$temp_arr['no_tax_price'] = $_POST['no_tax_price'];
	$temp_arr['update_time'] = $_POST['update_time'];
	$temp_arr['goods_num'] = $_POST['goods_count'];
	$temp_arr['goods_date'] = $_POST['goods_delivery'];
	$temp_arr['goods_desc'] = $_POST['goods_desc'];
	
	$arr[] = $temp_arr;
			
	$email = $_POST['send_email'];
	$receive_email = $_POST['receive_email'];
	
	/*
	// 查看效果
	$smarty->assign('entityList', $arr);
	$smarty->assign('receive_email', $receive_email);
	$smarty->assign('shop_name', $_CFG['shop_name']);
	$smarty->assign('send_date', local_date($_CFG['date_format']));

    assign_query_info();
    $smarty->display('goods_inquiry_query_mail.htm');
    */

	 //发送邮件
	$tpl = get_mail_template('send_goods_inquiry');
	$smarty->assign('entityList',       $arr);
	$smarty->assign('receive_email', $receive_email);
	$smarty->assign('shop_name', $_CFG['shop_name']);
	$smarty->assign('send_date', local_date($_CFG['date_format']));
	$content = $smarty->fetch('str:' . $tpl['template_content']);
	if(send_mail("供应商", $email, $tpl['template_subject'], $content, $tpl['is_html'])){
		$send_ok = 1; //发送成功
	} else {
		$send_ok = 0; //发送失败
	}

	$link[0]['text'] = $_LANG['goods_inquiry_list'];
	$link[0]['href'] = 'goods_inquiry.php?act=goods_inquiry_list';
	
	if($send_ok == 1) {
		// 发送成功后将数据从询价列表中删除
	    $id = intval($_POST['id']);
	    $sql = "DELETE FROM ".$ecs->table('goods_inquiry_list')." where id = $id ";
	    $db->query($sql);
    
		$note = $_LANG['goods_inquiry_email_succed'];
		sys_msg($note, 0, $link); //成功自动跳转
	} else {
		$note = $_LANG['goods_inquiry_email_fail'];
		
		$link[1]['text'] = $_LANG['history_back'];
		$link[1]['href'] = 'javascript:history.go(-1)';
		
		sys_msg($note, 0, $link, false); //失败时显示错误信息并停止跳转
	}
}


/**
 * 获取询价商品列表
 *
 * @access  public
 * @param $query_str 在现在的基础上增加的查询条件
 * @param $page_size 分页显示条数，为0时表示不分页，直接取全部信息
 * @param $is_history_query 是否为历史查询，为true时表示为历史查询，刚不需要增加限制显示条件
 * @author wonder
 * @return  array
 */
function getGoodsInquiryList($query_str = '', $page_size = 0, $is_history_query = false)
{
	/*
	 * 完整的查询语句如下，同个商品只查最新的一条：
		select a.* from ecs_goods_inquiry a where not exists(select 1 from ecs_goods_inquiry where 
		goods_name = a.goods_name and nation = a.nation and brand_name = a.brand_name and specification = a.specification and model = a.model and order_code = a.order_code and materials_type = a.materials_type 
		and id > a.id)
	 * 
	 */
	global $link_condition;
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
        
        // 查询条件
        // $filter['history_query'] = empty($_REQUEST['history_query']) ? '' : trim($_REQUEST['history_query']);
        
        if($is_history_query) {
        	$filter['history_query'] = 'history_query';
        }
        
        $where = "";
        
		$filter['nation'] = empty($_REQUEST['nation']) ? '' : trim($_REQUEST['nation']);
		$filter['brand_name'] = empty($_REQUEST['brand_name']) ? '' : trim($_REQUEST['brand_name']);
		$filter['specification'] = empty($_REQUEST['specification']) ? '' : trim($_REQUEST['specification']);
		$filter['model'] = empty($_REQUEST['model']) ? '' : trim($_REQUEST['model']);
		
		if ($filter['nation'] == true)
		{
			
			if(is_numeric($filter['nation'])){
				$nation_sql = "SELECT cat_name FROM " . $GLOBALS['ecs']->table('category'). " WHERE cat_id = ".$filter['nation'];
				$nation = $GLOBALS['db']->getOne($nation_sql);
				$filter['nation']  = $nation;
			}
			
			$where .= " AND nation LIKE '%" . mysql_like_quote($filter['nation']) . "%'";
		}
		if ($filter['brand_name'] == true)
		{
			$where .= " AND brand_name LIKE '%" . mysql_like_quote($filter['brand_name']) . "%'";
		}
		if ($filter['specification'] == true)
		{
			$where .= " AND specification LIKE '%" . mysql_like_quote($filter['specification']) . "%'";
		}
		if ($filter['model'] == true)
		{
			$where .= " AND model LIKE '%" . mysql_like_quote($filter['model']) . "%'";
		}

		$where .= $query_str;
		
        /* 记录总数以及页数
        if (isset($_POST['brand_name']))
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('goods_inquiry') .' WHERE brand_name = \''.$_POST['brand_name'].'\'';
        }
        else
        {
            $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('goods_inquiry');
        }
       */
        
        if($page_size == 0) {
	        $sql = "select count(*) from ".$GLOBALS['ecs']->table('goods_inquiry')." a where 1=1 ";
	        
	        if(!$is_history_query) {
	        	$sql .= " and not exists(select 1 from ecs_goods_inquiry where $link_condition id > a.id) ";
	        }
	        $sql .= $where;
	
	        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
	
	        $filter = page_and_size($filter);
        }
        
        /* 查询记录 
        if (isset($_POST['brand_name']))
        {
            if(strtoupper(EC_CHARSET) == 'GBK')
            {
                $keyword = iconv("UTF-8", "gb2312", $_POST['brand_name']);
            }
            else
            {
                $keyword = $_POST['brand_name'];
            }
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('goods_inquiry')." WHERE brand_name like '%{$keyword}%' ORDER BY sort_order ASC";
        }
        else
        {
            $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('goods_inquiry')." ORDER BY sort_order ASC";
        }
        */
        
        $sql = "select a.* from ".$GLOBALS['ecs']->table('goods_inquiry')." a where 1=1 ";
        $order_str = " order by a.id desc ";
        
        if(!$is_history_query) {
	        	$sql .= " and not exists(select 1 from ecs_goods_inquiry where $link_condition id > a.id) ";
	        	//$order_str = " order by a.goods_name desc ";
	        }
	    $sql .= $where.$order_str;

        if($page_size == 0) {
        	set_filter($filter, $sql);
        }
        
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    
    if($page_size == 0) {
    	$res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    } else { 
    	$res = $GLOBALS['db']->selectLimit($sql, $page_size, 0);
    }
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        //$brand_logo = empty($rows['brand_logo']) ? '' :
        //    '<a href="../' . DATA_DIR . '/brandlogo/'.$rows['brand_logo'].'" target="_brank"><img src="images/picflag.gif" width="16" height="16" border="0" alt='.$GLOBALS['_LANG']['brand_logo'].' /></a>';
        //$site_url   = empty($rows['site_url']) ? 'N/A' : '<a href="'.$rows['site_url'].'" target="_brank">'.$rows['site_url'].'</a>';

        //$rows['brand_logo'] = $brand_logo;
        //$rows['site_url']   = $site_url;
        
    	//处理时间
    	global $_CFG;
   	 	$rows['update_time'] = local_date($_CFG['time_format'], $rows['update_time']);

        $arr[] = $rows;
    }

    return array('brand' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 
 * 获取用户添加的商品询价列表
 */
function getAddGoodsInquiryList(){
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
        
        // 查询条件
        $where = "";
        
		$filter['nation'] = empty($_REQUEST['nation']) ? '' : trim($_REQUEST['nation']);
		$filter['history_query'] = "goods_inquiry_list";
		$filter['brand_name'] = empty($_REQUEST['brand_name']) ? '' : trim($_REQUEST['brand_name']);
		$filter['specification'] = empty($_REQUEST['specification']) ? '' : trim($_REQUEST['specification']);
		$filter['model'] = empty($_REQUEST['model']) ? '' : trim($_REQUEST['model']);
		
		if ($filter['nation'] == true)
		{
			
			if(is_numeric($filter['nation'])){
				$nation_sql = "SELECT cat_name FROM " . $GLOBALS['ecs']->table('category'). " WHERE cat_id = ".$filter['nation'];
				$nation = $GLOBALS['db']->getOne($nation_sql);
				$filter['nation']  = $nation;
			}
			
			$where .= " AND nation LIKE '%" . mysql_like_quote($filter['nation']) . "%'";
		}
		if ($filter['brand_name'] == true)
		{
			$where .= " AND brand_name LIKE '%" . mysql_like_quote($filter['brand_name']) . "%'";
		}
		if ($filter['specification'] == true)
		{
			$where .= " AND specification LIKE '%" . mysql_like_quote($filter['specification']) . "%'";
		}
		if ($filter['model'] == true)
		{
			$where .= " AND model LIKE '%" . mysql_like_quote($filter['model']) . "%'";
		}

		$where .= $query_str;
		
        /* 记录总数以及页数*/
        if($page_size == 0) {
	        $sql = "select count(*) from ".$GLOBALS['ecs']->table('goods_inquiry_list')." a where 1=1 ";
	        
	        $sql .= $where;
	
	        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
	
	        $filter = page_and_size($filter);
        }
        
        $sql = "select a.* from ".$GLOBALS['ecs']->table('goods_inquiry_list')." a where 1=1 ";
        $order_str = " order by a.id desc ";
        
	    $sql .= $where.$order_str;

        if($page_size == 0) {
        	set_filter($filter, $sql);
        }
        
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    
    if($page_size == 0) {
    	$res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    } else { 
    	$res = $GLOBALS['db']->selectLimit($sql, $page_size, 0);
    }
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
    	//处理时间
    	global $_CFG;
   	 	$rows['update_time'] = local_date($_CFG['time_format'], $rows['update_time']);

        $arr[] = $rows;
    }

    return array('brand' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 获取品牌列表
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_cat_brand_list()
{
	$sql = "select brand_id as attr_id, brand_name as attr_name, cat_id from " . $GLOBALS['ecs']->table('brand'). 
 		   "ORDER BY cat_id ,sort_order";

    $arr = $GLOBALS['db']->getAll($sql);

    $list = array();

    foreach ($arr as $val)
    {
        $list[$val['cat_id']][] = array($val['attr_id']=>$val['attr_name']);
    }

    return $list;
}
?>
